<?php

/**
 * Created by PhpStorm.
 * User: bob
 * Date: 21/9/25
 * Time: 上午10:37
 */

namespace App\Api\Admin\Controllers;

use App\Api\Account\Services\BusinessService;
use App\Api\Admin\Models\Tag;
use App\Api\Web\Models\Comment;
use App\Api\Web\Models\CommentReply;
use App\Api\Web\Models\House;
use App\Api\Web\Models\HouseTag;
use App\Api\Web\Models\HouseWanted;
use App\Api\Web\Models\OrderHouse;
use App\Api\Web\Services\HouseService;
use App\Http\Controllers\Controller;
use App\User;
use Carbon\Carbon;
use function GuzzleHttp\Promise\promise_for;
use Illuminate\Http\Request;

class HouseController extends Controller
{
    public function releaseOrUpdate(Request $request)
    {
        $params = $request->all();

        $status = data_get($params, 'status', 200);
        $userId = data_get($params, 'user_id', '');
        $r = HouseService::release($params, 'admin', $userId);
        return $this->formatOutput($r);
    }

    public function houseDelete(Request $request)
    {
        $params = $request->all();

        $id = data_get($params, 'id', 0);
        $r = House::find($id)->update([
            'status' => '-900'
        ]);
        return $this->formatOutput($r);
    }

    public function index(Request $request)
    {
        $params = $request->all();
        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);
        $keywords = data_get($params, 'name', '');
        $houses = House::orderBy('id', 'desc')
            ->where('status', '>', -900)
            ->join('community as c', 'c.id', '=', 'house.community_id')
            ->select('house.*', 'c.name as community_name');

        if (!empty($keywords)) {
            $houses = $houses->where(function ($query) use ($keywords) {
                $query->where('c.name', 'like', "%$keywords%")
                    ->orwhere('c.near_buildings', 'like', "%$keywords%")
                    ->orwhere('c.near_bus_stop', 'like', "%$keywords%")
                    ->orwhere('c.near_company', 'like', "%$keywords%")
                    ->orwhere('c.keywords', 'like', "%$keywords%")
                    ->orwhere('c.near_site1', 'like', "%$keywords%")
                    ->orwhere('c.near_subway1', 'like', "%$keywords%")
                    ->orwhere('c.near_site2', 'like', "%$keywords%")
                    ->orwhere('c.near_subway2', 'like', "%$keywords%")
                    ->orwhere('c.business', 'like', "%$keywords%")
                    ->orwhere('c.near_area', 'like', "%$keywords%");
            });
        }

        $houses = $houses->paginate($pageSize, ['*'], 'page', $page)
        ->toArray();

        foreach ($houses['data'] as $k => $v) {
            $houses['data'][$k]['role_name'] = data_get(House::$role, data_get($v, 'role', ''), '未知');
            $houses['data'][$k]['rent_out_name'] = data_get(House::$rent_out, data_get($v, 'rent_out', ''), '未知');
            $houses['data'][$k]['toward_name'] = data_get(House::$toward, data_get($v, 'toward', ''), '未知');
            $houses['data'][$k]['facilities_name'] = data_get(House::$facilities, data_get($v, 'facilities', ''), '未知');
            $houses['data'][$k]['payment_account_name'] = data_get(House::$payment_account, data_get($v, 'payment_account', ''), '未知');
            $houses['data'][$k]['contract_name'] = data_get(House::$contract, data_get($v, 'contract'), '未知');
            $houses['data'][$k]['is_has_free_name'] = data_get(House::$is_has_free, data_get($v, 'is_has_free', ''), '未知');
            $houses['data'][$k]['is_check_name'] = data_get(House::$is_check, data_get($v, 'is_check', ''), '未知');
            $houses['data'][$k]['toilet_type_name'] = data_get(House::$toilet_type, data_get($v, 'toilet_type', ''), '未知');
            $houses['data'][$k]['bedroom_type_name'] = data_get(House::$bedroom_type, data_get($v, 'bedroom_type', ''), '未知');
            $houses['data'][$k]['sex_limit_name'] = data_get(House::$sex_limit, data_get($v, 'sex_limit', ''), '未知');
            $houses['data'][$k]['photos'] = json_decode(data_get($v, 'photos', ''));

        }

        return $this->formatOutput($houses);
    }

    public function houseById(Request $request)
    {
        $params = $request->all();
        $id = data_get($params, 'id', '');
        $v = House::orderBy('house.id', 'desc')
            ->where('house.id', '=', $id)
            ->leftJoin('community as c', 'c.id', '=', 'house.community_id')
            ->leftJoin('site as s', 's.id', '=', 'c.subway_site_id')
            ->select('house.*', 'c.name as community_name', 'c.longitude', 'c.latitude', 'c.near_bus_stop', 's.name as subway_site_name')
            ->first();

        if (!empty($v)) {
            $v->role_name = data_get(House::$role, data_get($v, 'role', ''), '未知');
            $v->rent_out_name = data_get(House::$rent_out, data_get($v, 'rent_out', ''), '未知');
            $v->toward_name = data_get(House::$toward, data_get($v, 'toward', ''), '未知');
            $v->facilities_name = data_get(House::$facilities, data_get($v, 'facilities', ''), '未知');
            $v->payment_account_name = data_get(House::$payment_account, data_get($v, 'payment_account', ''), '未知');
            $v->contract_name = data_get(House::$contract, data_get($v, 'contract'), '未知');
            $v->is_has_free_name = data_get(House::$is_has_free, data_get($v, 'is_has_free', ''), '未知');
            $v->is_check_name = data_get(House::$is_check, data_get($v, 'is_check', ''), '未知');
            $v->toilet_type_name = data_get(House::$toilet_type, data_get($v, 'toilet_type', ''), '未知');
            $v->bedroom_type_name = data_get(House::$bedroom_type, data_get($v, 'bedroom_type', ''), '未知');
            $v->sex_limit_name = data_get(House::$sex_limit, data_get($v, 'sex_limit', ''), '未知');
            $v->photos = json_decode(data_get($v, 'photos', ''));
            $user = User::find(data_get($v, 'user_id'));
            $v->user_name = data_get($user, 'name', '');
            $v->user_head_img = data_get($user, 'head_img', '');
//            $tags = HouseTag::select('tag.id', 'tag.name')
//                ->join('tag', 'tag.id', '=', 'house_tag.tag_id')
//                ->where('house_tag.house_id', '=', $v->id)
//                ->where('house_tag.tag_id', '>', 0)
//                ->where('tag.source', '=', 'house')
//                ->get()
//                ->toArray();
//            $vtag = [];
//            $vtagname = [];
//            print_r($vtagname);exit;
            $v->tag = HouseTag::select('tag.id', 'tag.name')
                ->join('tag', 'tag.id', '=', 'house_tag.tag_id')
                ->where('house_tag.house_id', '=', $v->id)
                ->where('house_tag.tag_id', '>', 0)
                ->where('tag.source', '=', 'house')
                ->pluck('tag.id');;
            $v->tag_name = HouseTag::select('tag.id', 'tag.name')
                ->join('tag', 'tag.id', '=', 'house_tag.tag_id')
                ->where('house_tag.house_id', '=', $v->id)
                ->where('house_tag.tag_id', '>', 0)
                ->where('tag.source', '=', 'house')
                ->pluck('tag.name');

            $r = \DB::table('house')->where('id', $id)->increment('pv');
//                ->pluck('tag.id', 'tag.name');
        }

        return $this->formatOutput($v);
    }

    public function tagIndex(Request $request)
    {
        $tag = Tag::where('source', '=', 'house')
            ->get();

        return $this->formatOutput($tag);
    }

    public function tagCreateOrUpdate(Request $request)
    {
        $params = $request->all();
        $id = data_get($params, 'id', '');
        $name = data_get($params, 'name', '');
        if (empty($id)) {
            $r = Tag::create([
                'name' => $name,
                'source' => 'house',
            ]);
        } else {
            $updates = [];
            empty($name) ?: $updates['name'] = $name;
            $r = 1;
            if (!empty($updates)) {
                $r = Tag::find($id)
                    ->update($updates);
            }
        }

        return $this->formatOutput($r);
    }

    public function tagDelete(Request $request)
    {
        $params = $request->all();
        $id = data_get($params, 'id', '');
        Tag::find($id)->delete();
        return $this->formatOutput('success');
    }

    public function houseWantedIndex(Request $request)
    {
        $params = $request->all();
        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);
        $housesWanted = HouseWanted::orderBy('id', 'desc')
            ->where('status', '>', -900)
            ->paginate($pageSize, ['*'], 'page', $page)
            ->toArray();

        foreach ($housesWanted['data'] as $k => $v) {
            $housesWanted['data'][$k]['rent_out_name'] = data_get(HouseWanted::$rent_out, data_get($v, 'rent_out', ''), '未知');
            $housesWanted['data'][$k]['toilet_type_name'] = data_get(HouseWanted::$toilet_type, data_get($v, 'toilet_type', ''), '未知');
            $housesWanted['data'][$k]['bedroom_type_name'] = data_get(HouseWanted::$bedroom_type, data_get($v, 'bedroom_type', ''), '未知');
        }

        return $this->formatOutput($housesWanted);
    }


    public function houseWantedOne(Request $request)
    {
        $params = $request->all();
        $id = data_get($params, 'id');
        $housesWanted = HouseWanted::where('id', '=', $id)
            ->first();

        if (!empty($housesWanted)) {
            $user = User::find(data_get($housesWanted, 'user_id'));
            $housesWanted->user_name = data_get($user, 'name', '');
            $housesWanted->user_head_img = data_get($user, 'head_img', '');
            $housesWanted->rent_out_name = data_get(HouseWanted::$rent_out, data_get($housesWanted, 'rent_out', ''), '未知');
            $housesWanted->toilet_type_name = data_get(HouseWanted::$toilet_type, data_get($housesWanted, 'toilet_type', ''), '未知');
            $housesWanted->bedroom_type_name = data_get(HouseWanted::$bedroom_type, data_get($housesWanted, 'bedroom_type', ''), '未知');
            $r = \DB::table('house_wanted')->where('id', $id)->increment('pv');
        }
        return $this->formatOutput($housesWanted);
    }

    public function houseWantedRelease(Request $request)
    {
        $params = $request->all();
        $userId = data_get($params, 'user_id', '');
        $r = HouseService::houseWantedReleaseOrUpdate($params, 'admin', $userId);

        return $this->formatOutput($r);
    }

    public function houseWantedDelete(Request $request)
    {
        $params = $request->all();

        $id = data_get($params, 'id', 0);
        $r = HouseWanted::find($id)->update([
            'status' => -900
        ]);
        return $this->formatOutput($r);
    }

    public function commentIndex(Request $request)
    {
        $params = $request->all();
        $pageSize = data_get($params, 'page_size', 20);
        $page = data_get($params, 'page', 1);
        $type = data_get($params, 'type', 1);
        $text = data_get($params, 'text', '');

        switch ($type) {
            case 1:
                $r = Comment::orderBy('comment.id', 'desc')
                    ->leftJoin('users as u', 'comment.user_id', '=', 'u.id')
                    ->select('comment.*', 'u.name', 'u.head_img');
                if (!empty($text)) {
                    $r = $r->where('comment.text', 'like', "%$text%");
                }

                $r = $r->paginate($pageSize, ['*'], 'page', $page)
                    ->toArray();
                break;
            case 2:
                $r = CommentReply::orderBy('comment_reply.id', 'desc')
                    ->leftJoin('users as u', 'comment_reply.user_id', '=', 'u.id')
                    ->select('comment_reply.*', 'u.name', 'u.head_img');
                if (!empty($text)) {
                    $r = $r->where('comment_reply.text', 'like', "%$text%");
                }
                $r = $r->paginate($pageSize, ['*'], 'page', $page)
                    ->toArray();
                break;
            default:
                throw new \LogicException('缺少必要参数', 100);
        }


        return $this->formatOutput($r);
    }

    public function commentDelete(Request $request)
    {
        $params = $request->all();
        $id = data_get($params, 'id', '');
        if (empty($id)) {
            throw new \LogicException('缺少必要参数', 100);
        }

        $type = data_get($params, 'type', 1);

        switch ($type) {
            case 1:
                Comment::find($id)->delete();
                break;
            case 2:
                CommentReply::find($id)->delete();
                break;
            default:
                throw new \LogicException('缺少必要参数', 100);
        }

        return $this->formatOutput('success');
    }
}
